<?php
include_once("controller/Bproduct.class.php");
include_once("controller/Bcategory.class.php");
include_once("controller/Paging.class.php");
require_once("model/category.class.php");
require_once("model/user.class.php");
require_once("model/page.class.php");
require_once("model/product.class.php");
require_once("model/news.class.php");
include_once("controller/upload.class.php");
include_once("includes/util.php");


/**
 * manage class. Process for all function in manage tab of admin page
 */
class Bpage 
{
	var $db;
 	var $smarty;
 	
    function Bpage($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/**
	 * Process ajax call for add new page
	 */
	function addNewPage()
	{
		/*if(isset($_POST['page_type']) && !empty($_POST['page_type']))
    		$existed = page::checkPagePositionExist($this->db, $_POST['page_type']);
		*/
			if($_FILES['page_img']['name'] != null && $_FILES['page_img']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['page_img']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_logo($_FILES['page_img']['tmp_name'], $namefile['0'], $image_type);
				} 
			}
			$page = $_POST;
			$page['page_img']= $imagename;
			$res = page::savePage($this->db, $page);
			$msg = "Page has been saved.";
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=manage&sub1=page") ;
		/*
		else
		{
			$res = -99;
		}*/
		
		/*if(!isset($_POST['ajax']))
		{
			if($res > 0)
				$_SESSION['page_message'] = "Page has been saved.";
			
			else
				$_SESSION['page_message'] = "Page doesn't saved.";
			$this->showPage();
			header("Location: ./?mod=manage&sub1=page");
		}
		else
		{
			$arr = array('code' => 1, 'message' => "");
			if($res > 0)
			{
				$arr['code'] = $res;
				$arr['message'] = _("Page has been saved.");
			}
			else if($res == -99)
			{
				$arr['code'] = $res;
				$arr['message'] = _("Page with selected header position existed.");
			}
			else
			{
				$arr['code'] = -1;
				$arr['message'] = _("Page doesn't saved.");
			}
			echo array2json($arr);
		}
		setcookie("message", $msg,time()+5);
		header("Location: ./?mod=manage&sub1=page") ;*/
	}
	
	/**
	 * Process ajax call for delete a page
	 */
	function deletePage()
	{
		$arr = array("id" => -1, "code" => -1, 
					"message" => _("Page doesn't deleted."));
		
		$id = "";
		if(isset($_POST['id']))
		{
			$id = $_POST['id'];
			
			$res = page::deletePage($this->db, $id);
			
			if($res > 0)
			{
				$arr['code'] = 0;
				$arr['message'] = _("Page has been deleted!");
			}
		}
		
		echo array2json($arr);
	}
	
	/**
	 * Process ajax call for update a page
	 */
	function updatePage()
	{
		/*if(isset($_POST['page_type']) && !empty($_POST['page_type']))
    	{
    		$existed = page::checkPagePositionExist($this->db, $_POST['page_type']);
    	}*/
    	
    	//if(($existed == null) || 
    		//($existed->page_id == $_POST['id']))
    	//{
			$pageID = page::getPageById($this->db, $_POST['id']);
		if($_FILES['page_img']['name'] != null && $_FILES['page_img']['name'] != "") {
			$upload = new JKUpload();
			$image = $_FILES['page_img']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_logo($_FILES['page_img']['tmp_name'], $namefile['0'], $image_type);
				@unlink("../".UPLOAD_DIR_LOGO.$pageID->page_img);
			} else {
				$_SESSION['file_error'] = $this->l['error']['imageError'];
			}
		}
		else
			$imagename = $pageID->page_img;
			$page = $_POST;
			$page['page_img']= $imagename;	
			$res = page::updatePage($this->db, $page);
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=manage&sub1=page") ;
    	//}
    	/*else
    	{
    		$res = -99;
    	}*/
		
		if(!isset($_POST['ajax']))
		{
			if($res == 0)
			{
				$_SESSION['page_message'] = "Page has been updated.";
			}
			else if($res == -99)
			{
				$_SESSION['page_message'] = "Page with selected header position existed.";
			}
			else
			{
				$_SESSION['page_message'] = "Page doesn't updated.";
			}
			header("Location: ./?mod=manage&sub1=page");
			$this->showPage();
		}
		else
		{
			$arr = array('code' => 1, 'message' => "");
			
			if($res == 0)
			{
				$arr['code'] = 0;
				$arr['message'] = _("Page has been saved.");
			}
			else if($res == -99)
			{
				$arr['code'] = -99;
				$arr['message'] = _("Page with selected header position existed.");
			}
			else
			{
				$arr['code'] = -1;
				$arr['message'] = _("Page could not be saved.");
			}
			
			echo array2json($arr);
		}
	}
}
?>